package com.itqf.controller;

import com.itqf.annotation.RequiresPermissionsDesc;
import com.itqf.bean.DtsTopic;
import com.itqf.dto.DtsSearchDto;
import com.itqf.service.DtsTopicService;
import com.itqf.util.ResponseUtil;
import com.itqf.utils.AdminResponseCode;
import com.itqf.utils.AdminResponseUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * projectName: shop_manage
 *
 * @author: MaTianWei
 * time: 2021/7/17 16:20
 * description:
 */
@RestController
@CrossOrigin("*")
@RequestMapping("admin/topic")
public class AdminSpecialController {
    @Autowired
    private DtsTopicService dtsTopicService;
    public static final Logger logger = LoggerFactory.getLogger(AdminAuthController.class);

    //专题列表显示
    @RequiresPermissions("admin:topic:list")
    @RequiresPermissionsDesc(menu = {"推广管理","专题管理"},button = "专题列表展示")
    @GetMapping("list")
    public  Object list(DtsSearchDto serachDto){
        logger.debug("[专题管理]列表查询参数:{}", serachDto);
        Map data = dtsTopicService.findByPage(serachDto);
        logger.debug("[专题管理]分页查询数据为",data);
        return ResponseUtil.ok(data);
    }


     //专题添加
    @RequiresPermissions("admin:topic:create")
    @RequiresPermissionsDesc(menu = {"推广管理","专题管理"},button = "添加专题")
    @PostMapping("create")
    public Object addTopic(@RequestBody DtsTopic dtsTopic) {
        logger.debug("[添加专题]查询参数:{}", dtsTopic);
        //先进行判断
        String topic = dtsTopic.getTitle();
        if (topic == null || topic == "") {
            logger.warn("[添加专题]要添加的专题为null");
            return ResponseUtil.fail();
        }
        boolean hasTopic = dtsTopicService.checkTopic(topic);
        if (hasTopic) {
            logger.error("[添加专题]该专题已经添加过", topic);
            return AdminResponseUtil.fail(AdminResponseCode.ARTICLE_NAME_EXIST);
        }
        dtsTopic = dtsTopicService.saveTopic(dtsTopic);

        //返回结果
        logger.info("[添加专题]接口调用结束!获取回显数据为:{}", dtsTopic);
        return ResponseUtil.ok(dtsTopic);
    }






    //删除专题
    @RequiresPermissions("admin:topic:delete")
    @RequiresPermissionsDesc(menu = {"推广管理","专题管理"},button = "删除专题")
    @PostMapping("delete")
    public Object deleteById(@RequestBody DtsTopic dtsTopic){

        logger.debug("[删除专题],传入的id为:{}",dtsTopic.getId());
        int rows = dtsTopicService.deleteById(dtsTopic.getId());
        if (rows == 0) {
            return ResponseUtil.fail();
        }
        logger.info("[删除专题]删除影响行数为:{}",rows);

        return ResponseUtil.ok();
    }







     //修改专题信息
    @RequiresPermissions("admin:topic:update")
    @RequiresPermissionsDesc(menu = {"推广管理","专题管理"},button = "修改专题信息")
    @PostMapping("update")
    public Object update(@RequestBody DtsTopic dtsTopic){

        int rows=dtsTopicService.updateTopic(dtsTopic);
        if (rows == 0){
            return ResponseUtil.fail();
        }

        return ResponseUtil.ok(dtsTopic);
    }
}